'''
Created on 09/03/2011

@author: andrew
'''
from MusicFile import MusicFile
from mutagen.oggvorbis import OggVorbis

class OGGfile(MusicFile):
    '''
    Extract OGG informations.
    Install dependencies:
        yum install python-mutagen
    '''
    def __init__(self,path):
        '''
        Constructor
        '''
        MusicFile.__init__(self,path)
        try:
            self.ogg = OggVorbis(path)
        except:
            raise ValueError('Invalid OGG file: '+self.path)
            
    @property
    def album(self):
        return self.ogg['album'][0] if self.ogg.has_key('album') else ""

    @album.setter
    def album(self,album):
        if not(self.writable): raise IOError("Permission denied") 
        self.ogg["album"] = album
        self.ogg.save()

    @property
    def artist(self):
        return self.ogg['artist'][0] if self.ogg.has_key('artist') else ""

    @artist.setter
    def artist(self,artist):
        if not(self.writable): raise IOError("Permission denied") 
        self.ogg["artist"] = artist
        self.ogg.save()

    @property
    def title(self):
        return self.ogg['title'][0] if self.ogg.has_key('title') else ""

    @title.setter
    def title(self,title):
        if not(self.writable): raise IOError("Permission denied") 
        self.ogg["title"] = title
        self.ogg.save()

    @property
    def sampleRate(self):
        return self.ogg.info.sample_rate

    @property
    def bitRate(self):
        return self.ogg.info.bitrate / 1000

    @property
    def length(self):
        secs = int(self.ogg.info.length)
        mins = secs / 60
        secs = secs - mins * 60
        return "%d:%02d" % (mins, secs)

    def __repr__(self):
        for k, v in self.ogg.items():
            print k, ":", v
        return ""
